package com.oceanstar.controller;

import com.oceanstar.Pojo.*;
import com.oceanstar.service.IHealthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*")
public class HealthController {

    @Autowired
    private IHealthService healthService;

    /*
     *管理员上传老人的健康数据
     */
    @PostMapping("/health/record")
    public Result updateHealthRecord(@RequestBody HealthInfo healthInfo, @RequestHeader("token") String token){
        try {
            healthService.updateHealthRecord(healthInfo,token);
            return Result.success();
        } catch (Exception e) {
            return Result.error("上传失败: " + e.getMessage());
        }
    }

    /*
     *用于查询老人的心率数据
     */
    @GetMapping("/health/heartRate/history")
    public Result getHeartRateHistory(@RequestParam("id") Integer id, @RequestHeader("token") String token) {
        try {
            HealthHistoryDTO historyDTO = healthService.getHealthHistory(id, "heartRate", token);
            return Result.success(historyDTO);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /*
     *用于查询老人的血压数据
     */
    @GetMapping("/health/bloodPressure/history")
    public Result getBloodPressureHistory(@RequestParam("id") Integer id, @RequestHeader("token") String token) {
        try {
            HealthHistoryDTO historyDTO = healthService.getHealthHistory(id, "bloodPressure", token);
            return Result.success(historyDTO);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /*
     *用于查询老人的血糖数据
     */
    @GetMapping("/health/bloodSugar/history")
    public Result getBloodSugarHistory(@RequestParam("id") Integer id, @RequestHeader("token") String token) {
        try {
            HealthHistoryDTO historyDTO = healthService.getHealthHistory(id, "bloodSugar", token);
            return Result.success(historyDTO);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /*
     *用于查询老人的体重数据
     */
    @GetMapping("/health/weight/history")
    public Result getWeightHistory(@RequestParam("id") Integer id, @RequestHeader("token") String token) {
        try {
            HealthHistoryDTO historyDTO = healthService.getHealthHistory(id, "weight", token);
            return Result.success(historyDTO);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /*
     *用于查询老人的医疗记录数据
     */
    @GetMapping("/health/medicalHistory/history")
    public Result getMedicalHistoryHistory(@RequestParam("id") Integer id, @RequestHeader("token") String token) {
        try {
            HealthHistoryDTO historyDTO = healthService.getHealthHistory(id, "medicalHistory", token);
            return Result.success(historyDTO);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /*
     *用于查询老人的过敏数据
     */
    @GetMapping("/health/allergyHistory/history")
    public Result getAllergyHistoryHistory(@RequestParam("id") Integer id, @RequestHeader("token") String token) {
        try {
            HealthHistoryDTO historyDTO = healthService.getHealthHistory(id, "allergyHistory", token);
            return Result.success(historyDTO);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /*
     *手动标记/取消标记老人为重点关注对象。
     */
    @PostMapping("/focus/mark")
    public Result focusMark(@RequestBody MarkMessage message, @RequestHeader("token") String token) {
        try{
            healthService.MarkElder(message, token);
            return Result.success();
        } catch (Exception e) {
            return Result.error("操作失败: " + e.getMessage());
        }
    }

    /*
     *获取重点关注老人名单列表。
     */
    @GetMapping("/focus/mark")
    public Result getMarkedElder(@RequestParam("page") int page,@RequestParam("pageSize") int pageSize, @RequestHeader("token") String token) {
        try {
            PageInfo<MarkedElderDTO> pageInfo = healthService.getMarkedElderList(page, pageSize);
            return Result.success(pageInfo);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
}